package com.bn.feedMaster;

import java.util.List;

import javax.transaction.TransactionManager;

import atg.adapter.gsa.ChangeAwareList;
import atg.dtm.TransactionDemarcation;
import atg.dtm.TransactionDemarcationException;
import atg.epub.project.Process;
import atg.nucleus.GenericService;
import atg.repository.MutableRepository;
import atg.repository.MutableRepositoryItem;
import atg.repository.Repository;
import atg.repository.RepositoryException;
import atg.repository.RepositoryItem;

public class TestMasterFeedsProcessor extends GenericService{

	/** The transaction manager. */
    private TransactionManager transactionManager;
    private static final String FEED_NAME = "skuOn30";
    private Repository productCatalog;
    private FeedsTools feedsTools;
    private String testProductId;
	private String productName;
	private String skuDisplayName;
	private String skuId;
	
	public void processData(){
		TransactionDemarcation td = null;
		TransactionManager tm = getTransactionManager();
		Process process = null;
		StringBuilder errorsBuffer = new StringBuilder();
		td = new TransactionDemarcation();
		boolean rollback = false;
		try{
			td.begin(tm, TransactionDemarcation.REQUIRES_NEW);
			process = getFeedsTools().startProjectInBCC(errorsBuffer, FEED_NAME);
			setProductDetails();
			getFeedsTools().advanceWorkflow(process, errorsBuffer);
			
		}catch (Exception e) {
			if (isLoggingError()) {
				rollback = true;
				logError(FEED_NAME + " :: Exception occcured while Ending transaction", e);
            }
		}finally {
        	try {
                if (td != null)
                    td.end(rollback);
            } catch (TransactionDemarcationException tde) {
            	 logError(tde);
                if (isLoggingError()) {
                    logError(FEED_NAME + " :: Exception occcured while Ending transaction", tde);
                }
            }
        }
	}
	
	
	private void setProductDetails() {
		MutableRepository mutableRepository = (MutableRepository) productCatalog;
		MutableRepositoryItem mutableRepositoryItem = null;
		try {
				BNFeedConstants constants = new BNFeedConstants();
				if(mutableRepository != null){
					mutableRepositoryItem = mutableRepository.createItem(getTestProductId(),constants.PRODUCT_ITEM_DESC_NAME);
					mutableRepositoryItem.setPropertyValue(constants.PRODUCT_PROPERTY_DISPLAY_NAME, getProductName());
					RepositoryItem skuItem = createSku (mutableRepository,constants);
				    ChangeAwareList childSkus = (ChangeAwareList) mutableRepositoryItem.getPropertyValue(constants.SKU_CHILD_SKU);
				    if(skuItem != null){
				    	childSkus.add(skuItem);
				    	mutableRepositoryItem.setPropertyValue(constants.SKU_CHILD_SKU, childSkus);
				    }else{
				    	mutableRepositoryItem = mutableRepository.createItem(getSkuId(),constants.SKU_ITEM_DESC_NAME);
				    }
				}
				mutableRepository.addItem(mutableRepositoryItem);
				
		} catch (RepositoryException e) {
			if (isLoggingError()) {
                logError("Error while setting default shipping address", e);
            }
		}
		
	}
	
	 private RepositoryItem createSku(MutableRepository mutableRepository,BNFeedConstants constants) throws RepositoryException
	    {

	        RepositoryItem skuItem = mutableRepository.getItem(getSkuId(), constants.SKU_ITEM_DESC_NAME);
	        if(skuItem != null)
	        {
	            return skuItem;
	        }
	        MutableRepositoryItem item = mutableRepository.createItem(getSkuId(), constants.SKU_ITEM_DESC_NAME);
	        item.setPropertyValue(constants.SKU_DISPLAY_NAME, getSkuDisplayName());
	        mutableRepository.addItem(item);
	        return item;
	    }
	
	public Repository getProductCatalog() {
		return productCatalog;
	}

	public void setProductCatalog(Repository productCatalog) {
		this.productCatalog = productCatalog;
	}
	
	public TransactionManager getTransactionManager() {
		return transactionManager;
	}

	public void setTransactionManager(TransactionManager transactionManager) {
		this.transactionManager = transactionManager;
	}
	
	public FeedsTools getFeedsTools() {
		return feedsTools;
	}

	public void setFeedsTools(FeedsTools feedsTools) {
		this.feedsTools = feedsTools;
	}
	
	public String getTestProductId() {
		return testProductId;
	}
	
	public void setTestProductId(String testProductId) {
		this.testProductId = testProductId;
	}
	
	public String getProductName() {
		return productName;
	}
	
	public void setProductName(String productName) {
		this.productName = productName;
	}
	
	public String getSkuDisplayName() {
		return skuDisplayName;
	}

	public void setSkuDisplayName(String skuDisplayName) {
		this.skuDisplayName = skuDisplayName;
	}

	public String getSkuId() {
		return skuId;
	}

	public void setSkuId(String skuId) {
		this.skuId = skuId;
	}

}
